//
//  HomeTableViewController.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/10/16.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "HomeTableViewController.h"
#import "AccountViewModel.h"
#import "SVProgressHUD.h"
#import "AFNetworking.h"
#import "Status.h"
#import "User.h"
#import "StatusCell.h"
static NSString *identer = @"identer";

@interface HomeTableViewController ()

@property(nonatomic,strong)UIActivityIndicatorView *indicatorView;

@end

@implementation HomeTableViewController


/*
 
 注意在控制器之中视图方法的调用顺序,从上往下以此执行
 loadView
 
 viewDidLoad 这个方法只会调用一次
 viewWillAppear
 注意这个viewDidAppear方法每次视图出现都会调用,会重复多次调用
 viewDidAppear
 viewWillDisappear
 viewDidDisappear
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    //    用户未登陆的情况下显示登陆页面
    if((self.isUserLogin == false)){
        [self.loginView setUpInfoWithlabelText:@"关注这里,回家里看看这里有什么惊喜等着你" andimageName:nil];
        return;
    }
    
    [self prepareForTableView];
    
    //    定时刷新接口数据,根据返回的值修改值
    self.tabBarItem.badgeValue = @"1";
    
}
- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:YES];
    //    这样可以不用发完微博再进行手动刷新
    [self loadDate];
}

- (void)prepareForTableView{
    [self.tableView registerClass:[StatusCell class] forCellReuseIdentifier:identer];
    //    要使得tableview能够自动计算行高需要设定两个参数,即1预估行高和 2设置行高为自动计算
    self.tableView.estimatedRowHeight = 350;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    //    设置tableview的样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    WBRefreshControl *freshcontrol = [[WBRefreshControl alloc]init];
    self.myRefrshControl = freshcontrol;
    [self.tableView addSubview:_myRefrshControl];
    //   当WBRefreshControl 中的方法sendActionsForControlEvents:UIControlEventValueChanged被调用之后,load方法也会被执行
    [_myRefrshControl addTarget:self action:@selector(loadDate) forControlEvents:UIControlEventValueChanged];
    
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    indicatorView.backgroundColor = [UIColor yellowColor];
    self.indicatorView = indicatorView;
    self.tableView.tableFooterView = _indicatorView;
    //设置点击顶部状态栏快速回到顶部
    self.tableView.scrollsToTop = true;
    
    UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, -64, ScreenWidth, 44)];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.backgroundColor = themColor;
    self.tipLabel = textLabel;
    [self.navigationController.navigationBar addSubview:_tipLabel];
    //    http://www.jianshu.com/p/3defc4030543
    [self.navigationController.navigationBar insertSubview:_tipLabel atIndex:0];
    
    
}

- (void)loadDate;{
    BOOL isindicatorViewAnimating = [_indicatorView isAnimating];
    NSLog(@"isindicatorViewAnimating = %d",isindicatorViewAnimating);
    //    切记,自定义的属性一定要实例化
    //    注意在模型中使用单例对象的好处,确保在每次刷新时是这一个对象在调用方法,使用alloc init创建的对象可不是一个,是每刷新一次重新生成的一个
    self.statusListViewModel = [StatusListViewModel sharedAccountModel];
    [self.statusListViewModel loadHomeDate:isindicatorViewAnimating finished:^(BOOL isSuccess) {
        
        [self.myRefrshControl endRefreshing];
        
        if(isSuccess){
        if (self.indicatorView.isAnimating) {
            
            //    加载完数据,底部的小菊花停止转动
            [self.indicatorView stopAnimating];
        }
            //    展现_tipLabel
            [self showTipLabel];
            self.lastCount = self.statusListViewModel.homeArray.count;
            [self.tableView reloadData];
        
        }
        else{
            [SVProgressHUD showErrorWithStatus:@"网络军正在睡觉"];
        }
    }];
}

- (void)showTipLabel{
    NSInteger delta =  self.statusListViewModel.homeArray.count - _lastCount;
    if (delta == 0) {
        _tipLabel.text = @"没有新数据";
        
    }
    _tipLabel.text = [NSString stringWithFormat:@"有%ld条新数据",(long)delta];
    CGRect rect = _tipLabel.frame;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.tipLabel.frame = CGRectMake(0, 44, ScreenWidth, 44);
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.25 delay:1 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
            self.tipLabel.frame = rect;
            
        } completion:nil];
    }];
    
}

//实现数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    return self.statusListViewModel.homeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:identer];
    if (cell == nil) {
        cell = [[StatusCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identer];
    }
    if ((indexPath.item == self.statusListViewModel.homeArray.count - 1)&&(![_indicatorView isAnimating])) {
        //        转动小菊花,开始加载数据,等数据加载完之后再停止转动
        [_indicatorView startAnimating];
        [self loadDate];
    }
    cell.status = self.statusListViewModel.homeArray[indexPath.row];
    return cell;
}


@end
